package com.file.system.controller.system;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.file.system.entity.res.BaseCode;
import com.file.system.entity.res.CommonResult;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.system.WebDictionary;
import com.file.system.entity.system.WebDictionaryVal;
import com.file.system.service.system.IWebDictionaryService;
import com.file.system.service.system.IWebDictionaryValService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

import static cn.hutool.core.util.ObjectUtil.isNotEmpty;


/**
 * 字典
 *
 * @author jch
 */
@RestController
@Validated
@RequestMapping("/web/dictionary")
public class WebDictionaryController {

    @Autowired
    private IWebDictionaryService service;
    @Autowired
    private IWebDictionaryValService iWebDictionaryValService;

    /**
     * 查询字典分页
     *
     * @param pageIndex 页数
     * @param pageSize  每页条数
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/page/{pageIndex}/{pageSize}")
    public CommonResult<DataPage<List<WebDictionary>>> page(@PathVariable
                                                              @Min(value = 1, message = "页数最小1")
                                                              Integer pageIndex,
                                                              @PathVariable
                                                              @Min(value = 1, message = "每页条数最小1")
                                                              @Max(value = 100, message = "每页条数最大100")
                                                              Integer pageSize,
                                                              @RequestBody @Validated WebDictionary webDictionary
    ) {
        QueryWrapper<WebDictionary> query = new QueryWrapper<>();
        //排序
        query.orderByAsc("a.sort");
        //判断是否删除
        query.eq("a.is_del", 0);
        //判断名称是否为空
        query.like(isNotEmpty(webDictionary.getName()), "a.name", webDictionary.getName());
        //判断编码是否为空
        query.like(isNotEmpty(webDictionary.getCode()), "a.code", webDictionary.getCode());
        //判断字典列表或树是否为空
        query.eq(isNotEmpty(webDictionary.getDictListOrTree()), "a.dict_list_or_tree", webDictionary.getDictListOrTree());
        //查询集合
        DataPage<List<WebDictionary>> dp = service.page(pageIndex, pageSize, query);
        return CommonResult.ok().setResult(dp);
    }


    /**
     * 通过ids查询字典集合
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/listByIds")
    public CommonResult<List<WebDictionary>> listByIds(@RequestBody List<Long> ids) {
        //查询集合
        List<WebDictionary> list = service.listByIds(ids);
        return CommonResult.ok().setResult(list);
    }

    /**
     * 查询所有非删除状态字典
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/listAll")
    public CommonResult<List<WebDictionary>> listAll() {
        //查询集合
        List<WebDictionary> list = service.listAll();
        return CommonResult.ok().setResult(list);
    }


    /**
     * 通过id查询字典
     *
     * @param id 字典id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/id/{id}")
    public CommonResult<WebDictionary> getById(@PathVariable
                                                  @Min(value = 1, message = "id长度最小1")
                                                  Long id) {
        //查询集合
        WebDictionary webDictionary = service.getById(id);
        return CommonResult.ok().setResult(webDictionary);
    }


    /**
     * 通过id删除字典
     *
     * @param id id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/remove/{id}")
    public CommonResult removeById(@PathVariable
                            @Min(value = 1, message = "id长度最小1")
                            Long id) {
       int count = iWebDictionaryValService.count(new QueryWrapper<WebDictionaryVal>()
                .eq("a.is_del", 0)
                .eq("a.fk_dictionary_id", id)
        );
        //判断字典下是否存在值
        if (count > 0) return CommonResult.fail().setText("字典下存在值");
        //删除字典
        boolean state = service.removeById(id);
        //返回
        return CommonResult.check(state);
    }


    /**
     * 通过id编辑字典
     *
     * @param webDictionary 字典
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/updateById")
    public CommonResult<WebDictionary> updateById(@RequestBody @Validated(WebDictionary.update.class) WebDictionary webDictionary) {
        //查询
        WebDictionary webDictionaryOld = service.getById(webDictionary.getId());
        //查询编码是否存在
        if (ObjectUtil.notEqual(webDictionary.getCode(), webDictionaryOld.getCode())) {
            int count = service.count(new QueryWrapper<WebDictionary>()
                    .eq("a.is_del", 0)
                    .eq("a.code", webDictionary.getCode())
            );
            if (count > 0) {
                return CommonResult.fail(BaseCode.Common.PARAM_CODE_EXIST).setResult(webDictionary);
            }
        }
        //编辑修改
        boolean state = service.updateById(webDictionary);
        return CommonResult.check(state).setResult(webDictionary);
    }


    /**
     * 添加字典
     *
     * @param webDictionary 字典
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/save")
    public CommonResult<WebDictionary> save(@RequestBody @Validated(WebDictionary.add.class) WebDictionary webDictionary) {
        //查询编码是否存在
        int count = service.count(new QueryWrapper<WebDictionary>()
                .eq("a.is_del", 0)
                .eq("a.code", webDictionary.getCode())
        );
        if (count > 0) {
            return CommonResult.fail(BaseCode.Common.PARAM_CODE_EXIST).setResult(webDictionary);
        }
        //添加字典
        boolean state = service.save(webDictionary);
        return CommonResult.check(state).setResult(webDictionary);
    }
}

